let vm = new Vue({
    el: "#app",
    data: {
        index: 0, // 左侧菜单的下标
        state: 0, // 上方订单分类下标
        title: "", // 表单显示的标题
        search: "",
        show: false, // 编辑用户收货地址表单是否显示
        value: false, // 设置为true只有一页时隐藏分页栏
        reverse: true, // 是否倒叙
        display: -1, // 控制修改订单的表单是否显示
        isLogin: false, // 当前是否有用户登录
        requestUrl: "", // 请求提交的路径
        tracksDisplay: -1,
        // 分页相关参数：开始----------
        total: 10, // 总记录数
        pageSize: 5, // 每页记录数
        currentPage: 1, // 当前是第几页
        pageSizes: [5, 10, 20, 50, 100],
        // 分页相关参数：结束-----------
        user: {}, // 用户的信息
        cart: { // 购物车信息
            money: 0,
            quantity: 0,
            productId: ""
        },
        rec: { // 收货信息
            id: "",
            name: "",
            phone: "",
            address: ""
        },
        distribution: {}, // 订单的配送信息
        recs: [], // 用户的收货地址
        states: [], // 订单状态
        orders: [], // 用户的订单
        histories: [], // 浏览历史
        stateHistory: [], // 订单状态历史
        array: ["待付款", "已付款"] // 订单状态
    },
    created: function () {
        let index = location.search.split("=")[1];

        if (index !== undefined) {
            this.index = index;
        }

        // 获取登录用户的信息
        axios.get("/user/getlogin").then(function (response) {
            vm.user = response.data;
            vm.isLogin = response.data !== "";
        });

        // 查询用户的浏览历史
        axios.get("/history/select").then(function (response) {
            vm.histories = response.data;
        });

        // 获取用户的全部订单
        axios.get("/order/select", {
            params: {
                page: this.currentPage,
                size: this.pageSize
            }
        }).then(function (response) {
            let result = response.data;

            vm.total = result.total;
            vm.orders = result.rows;
        });

        // 获取用户的收货信息
        axios.get("/receive_info/selectByUserId").then(function (response) {
            vm.recs = response.data;
        });

        // 获取所有订单状态
        axios.get("/order_status/selectAll").then(function (response) {
            vm.states = response.data;
        });
    },
    methods: {
        query() {
            location.href = "/list.html?name=" + vm.search;
        },
        select(state) { // 查询用户订单列表
            vm.state = state;

            if (parseInt(state) === 0) {
                // 查询用户全部订单
                axios.get("/order/select", {
                    params: {
                        page: vm.currentPage,
                        size: vm.pageSize
                    }
                }).then(function (response) {
                    let result = response.data;

                    vm.total = result.total;
                    vm.orders = result.rows;
                });
            } else {
                // 通过订单状态查询用户订单
                axios.get("/order/selectByState", {
                    params: {
                        page: vm.currentPage,
                        size: vm.pageSize,
                        state: state
                    }
                }).then(function (response) {
                    let result = response.data;

                    vm.total = result.total;
                    vm.orders = result.rows;
                });
            }
        },
        logout() { // 安全退出
            axios.get("/user/logout").then(function (response) {
                let result = response.data;

                if (result.code === 200) {
                    location.href = "/login.html";
                } else {
                    vm.$notify({
                        title: '提示',
                        message: result.msg
                    });
                }
            });
        },
        pay(order_id) { // 去支付
            let total = 0;

            for (let i = 0; i < vm.orders.length; i++) {
                let order = vm.orders[i];

                if (order.id === order_id) {
                    total = order.total;
                }
            }

            vm.$prompt('订单一共需要支付' + total + '元，请输入支付密码：', '提示', {
                confirmButtonText: '确认支付',
                cancelButtonText: '还是算了',
            }).then(function(value) {
                axios.get("/order/pay", {
                    params: {
                        id: order_id
                    }
                }).then(function (response) {
                    let result = response.data;

                    if (result.code === 200) {
                        vm.$message({
                            type: 'success',
                            message: result.msg
                        });

                        vm.select(vm.state); // 重新查询订单
                    } else {
                        vm.$notify({
                            title: '提示',
                            message: result.msg
                        });
                    }
                }).catch();
            }).catch(function() {
                vm.$message({
                    type: 'info',
                    message: '您取消了支付'
                });
            });
        },
        cancel(id) { // 取消订单
            let bool = confirm("是否确认取消该订单？");

            if (bool) {
                axios.get("/order/cancel", {
                    params: {
                        id: id
                    }
                }).then(function (response) {
                    let result = response.data;

                    if (result.code === 200) {
                        // 成功提示
                        vm.$notify({
                            type: 'success',
                            title: '系统消息',
                            message: result.msg
                        });

                        vm.select(vm.state); // 重新查询订单
                    } else {
                        vm.$message({
                            type: 'error',
                            showClose: true,
                            message: result.msg
                        });
                    }
                });
            }
        },
        modify(order_id) { // 修改订单
            vm.display = order_id;

            axios.get("/distribution/selectByOrderId", {
                params: {
                    orderId: order_id
                }
            }).then(function (response) {
                vm.distribution = response.data;
            });
        },
        lookup(orderId) { // 查看物流
            // 通过订单号查询
            axios.get("/order_status_history/selectByOrderId", {
                params: {
                    orderId: orderId
                }
            }).then(function (response) {
                vm.stateHistory = response.data;
                vm.tracksDisplay = orderId;
            });
        },
        receive(order_id) { // 确认收货
            axios.get("/order/receipt", {
                params: {
                    id: order_id
                }
            }).then(function (response) {
                let result = response.data;

                if (result.code === 200) {
                    alert(result.msg);
                } else {
                    alert(result.msg);
                }
            });
        },
        comment(product_id) { // 去评价
            alert("去评价");
        },
        again(order_id) { // 再来一单
            axios.get("/order/again", {
                params: {
                    id: order_id
                }
            }).then(function (response) {
                let result = response.data;

                if (result.code === 200) {
                    // 成功提示
                    vm.$notify({
                        type: 'success',
                        title: '系统消息',
                        message: result.msg
                    });
                } else {
                    vm.$message({
                        type: 'error',
                        showClose: true,
                        message: result.msg
                    });
                }
            });
        },
        del(order_id) { // 删除订单
            let bool = confirm("是否确认删除该订单？");

            if (bool) {
                axios.get("/order/delete", {
                    params: {
                        id: order_id
                    }
                }).then(function (response) {
                    let result = response.data;

                    if (result.code === 200) {
                        // 成功提示
                        vm.$notify({
                            type: 'success',
                            title: '系统消息',
                            message: result.msg
                        });

                        // 重新加载订单列表
                        vm.select(vm.state);
                    } else {
                        // 错误提示
                        vm.$message({
                            type: 'error',
                            showClose: true,
                            message: result.msg
                        });
                    }
                });
            }
        },
        toCart(product_id) { // 点击浏览历史的商品添加到购物车
            location.href = '/html/detail.html?id=' + product_id;
        },
        ensure() { // 点击修改订单的确认按钮
            vm.$confirm('每个订单只允许修改一次，是否确认提交本次修改?', '提示', {
                confirmButtonText: '确定提交',
                cancelButtonText: '我再想想',
                type: 'warning'
            }).then(function(response) {
                axios.post("/distribution/updateById", vm.distribution).then(function (response) {
                    let result = response.data;

                    if (result.code === 200) {
                        vm.$notify({
                            type: 'success',
                            message: result.msg
                        });
                    } else {
                        vm.$message({
                            type: 'error',
                            showClose: true,
                            message: result.msg
                        });
                    }

                    vm.display = -1; // 设置显示-1，即所有表单均不显示
                });
            }).catch(function() {
                this.$message({
                    type: 'info',
                    message: '操作已取消'
                });
            });
        },
        add() { // 添加地址
            vm.title = "添加地址";
            vm.show = vm.show ? vm.show : true;
            vm.requestUrl = "/distribution/insert";
        },
        update() { // 修改地址
            vm.title = "修改地址";
            vm.show = vm.show ? vm.show : true;
            vm.requestUrl = "/distribution/updateById";
        },
        fill() { // 填充收货信息到表单
            for (let i = 0; i < vm.recs.length; i++) {
                if (vm.recs[i].id == vm.rec.id) {
                    vm.rec.name = vm.recs[i].name;
                    vm.rec.address = vm.recs[i].address;
                    vm.rec.phone = vm.recs[i].phone;
                }
            }
        },
        menuChange(index) { // 点击左侧菜单
            vm.index = index;
        },
        submit() { // 保存用户的收货信息
            if (vm.requestUrl == "") {
                vm.$message({
                    type: 'error',
                    showClose: true,
                    message: "提交路径不存在"
                });
            } else {
                axios.post(vm.requestUrl, vm.rec).then(function (response) {
                    let result = response.data;

                    if (result.code == 200) {
                        // 重新获取用户的收货信息
                        axios.get("/receive_info/selectByUserId").then(function (response) {
                            vm.recs = response.data;
                        });
                    } else {
                        vm.$message({
                            type: 'error',
                            showClose: true,
                            message: result.msg
                        });
                    }
                });
            }
        },
        handleSizeChange(val) {
            vm.pageSize = val;

            this.select(vm.state);
        },
        handleCurrentChange(val) {
            vm.currentPage = val;

            this.select(vm.state);
        }
    }
});